package cn.mandifal.login.service;

import cn.mandifal.pojo.DpSeller;
import cn.mandifal.seller.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {

    private SellerService sellerService;
    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //定义角色列表，声明只有 ROSE_SELLER的角色用户才能登陆
        List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        //得到商家
        DpSeller oneSeller = sellerService.findOneSeller(username);
        if (oneSeller !=null){
            if("1".equals(oneSeller.getStatus())){
                return new User(username,oneSeller.getPassword(), grantedAuths);

            }else {
                return null;
            }
        }else{
            return null;
        }

    }
}